package bb.lanxing.device.sgdevcie.xpair;

import android.content.Context;
import android.content.Intent;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.ActivityResultRegistry;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.common.messager.MessageSender;
import bb.lanxing.databinding.DataBindingAdapters;
import bb.lanxing.device.sgdevcie.xpair.uimodel.XPairIntroUIModel;
import bb.lanxing.device.sgdevcie.xpair.uimodel.XPairPairUIModel;
import bb.lanxing.device.sgdevcie.xpair.uimodel.XPairSearchUIModel;
import bb.lanxing.device.sgdevcie.xpair.uimodel.XPairUIModel;
import bb.lanxing.lib.devices.base.DeviceContext;
import bb.lanxing.util.Log;

public final class XPairLauncher implements DefaultLifecycleObserver {
    private static final String TAG = "XPairLauncher";
    public ActivityResultLauncher<Intent> launcherNav;
    private final Context originContext;
    private final ActivityResultRegistry registry;

    @Override
    public void onDestroy(@NonNull LifecycleOwner lifecycleOwner) {
    }

    @Override
    public void onPause(@NonNull LifecycleOwner lifecycleOwner) {
    }

    @Override
    public void onResume(@NonNull LifecycleOwner lifecycleOwner) {
    }

    @Override
    public void onStart(@NonNull LifecycleOwner lifecycleOwner) {
    }

    @Override
    public void onStop(@NonNull LifecycleOwner lifecycleOwner) {
    }

    public XPairLauncher(Context originContext, ActivityResultRegistry registry) {
        this.originContext = originContext;
        this.registry = registry;
    }

    public final ActivityResultLauncher<Intent> getLauncherNav() {
        ActivityResultLauncher<Intent> activityResultLauncher = this.launcherNav;
        if (activityResultLauncher != null) {
            return activityResultLauncher;
        }
        return null;
    }

    public final void setLauncherNav(ActivityResultLauncher<Intent> activityResultLauncher) {
        this.launcherNav = activityResultLauncher;
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        ActivityResultLauncher<Intent> register = this.registry.register("XPairLauncher", owner, new ActivityResultContracts.StartActivityForResult(), XPairLauncher::startForActivityResult);
        setLauncherNav(register);
    }

    public static void startForActivityResult(ActivityResult activityResult) {
        if (activityResult.getResultCode() == -1) {
            MessageSender messageSender = App.getContext().getMessageSender();
            Intent data = activityResult.getData();
            Log.d(TAG, "XPairLauncher...onCreate...");
            messageSender.sendMessageDelayed(R.id.show_device_home, 0, 0, null, data != null ? data.getExtras() : null, 0L);
        }
    }

    public final void launchForNav() {
        String string = App.getContext().getString(DataBindingAdapters.getDeviceNameResource(28));
        String string2 = App.getContext().getString(R.string.st_turn_on_bluetooth);
        String string3 = App.getContext().getString(R.string.st_make_device_pair_mode);
        String string4 = App.getContext().getString(R.string.st_close_to_pair_device, string);
        XPairIntroUIModel.BannerData bannerData1 = new XPairIntroUIModel.BannerData(R.drawable.ic_default, string2);
        XPairIntroUIModel.BannerData bannerData2 = new XPairIntroUIModel.BannerData(R.drawable.ic_default, string3);
        XPairIntroUIModel.BannerData bannerData3 = new XPairIntroUIModel.BannerData(R.drawable.ic_default, string4);

        List<XPairIntroUIModel.BannerData> uiBannerList = new ArrayList<>();
        uiBannerList.add(bannerData1);
        uiBannerList.add(bannerData2);
        uiBannerList.add(bannerData3);

        List<String> uiTipsList = new ArrayList<>();
        uiTipsList.add(App.getContext().getString(R.string.st_turn_on_bluetooth));
        uiTipsList.add(App.getContext().getString(R.string.st_ensure_s_nearby_and_in_pair_mode, string));

        XPairIntroUIModel xPairIntroUIModel = new XPairIntroUIModel(R.drawable.ic_intro_nav_pair, uiBannerList, uiTipsList, R.string.st_pair);

        List<String> uiSearchList = new ArrayList<>();
        uiSearchList.add(App.getContext().getString(R.string.st_turn_on_bluetooth));
        uiSearchList.add(App.getContext().getString(R.string.st_ensure_s_nearby_and_in_pair_mode, string));
        XPairSearchUIModel xPairSearchUIModel = new XPairSearchUIModel(28, uiSearchList);
        String string5 = App.getContext().getString(R.string.st_after_binding_s_will_beep, string);
        assert getLauncherNav() != null;
        XPairManagerActivity.Companion.launch(getLauncherNav(), this.originContext, new XPairUIModel(string, R.drawable.ic_default, xPairIntroUIModel, xPairSearchUIModel, new XPairPairUIModel(string5)));
        DeviceContext.stopAutoConnector();
    }
}
